Script commands
Note: This is work in progress... Please add. Fe50 01:21, December 28, 2010 (UTC) ---- =Additional uBASIC and Lua Scripting Commands= __TOC__ get_config_value Returns the value of the specified CHDK configuration value. Configuration values are normally set from the CHDK menu but can be set by this function by using the index number found in CHDK source file conf.c in the structure conf_info[]. Lua code : int1,int2,str1,table1=get_config_value(,*) int1 = integer values int1, int2 = positions of OSD elements (x,y) str1 = strings (e.g. a file path) table1 = fields nil = error (ID not found without setting = message value, when ID was not found id_max = get_config_value(0); id_max is the highest ID value in conf_info[] set_config_value Sets the value of the specified CHKD configuration value. Configuration values are selected by an index number found in CHDK source file conf.c in the structure conf_info[]. set_config_value(,int1,int2,str1,table1) Returns true(success) or false(error). get_movie_status Returns status of movie recording. can be used for stuff like checks instead of sleep commands. if get_movie_status is 0 or 1, movie recording is stopped or paused. if get_movie_status is 4, recording is in progress. if it is 5, recording is stopped but camera is still writing movie to sdcard. set_movie_status is experimental and so far only works on a720 & s3is properly. using this command you can pause, unpause and stop video recordings. set_movie_status 1 will pause movie (will not work on all cameras, typically they freeze or shutdown after 2 seconds !) set_movie_status 2 will unpause set_movie_status 3 will stop video recording (this should work on all types of cameras). get_video_button Return 1 when camera has a video button. Return 0 when camera has no video button. The returned value is based on the whether the platform_camera.h file for the camea indicates that it has a video button. get_display_mode returns Display mode (record mode only) (0 = show info icons, 1 = Do not show info icons, 2 = LCD off, 3 = EVF) regardless of Digic II or Digic III camera (one of the features to help get chdk scripts universal) get_propset Returns the propset, meaning which set of propsets should work for your camera in regards to Digic II / Digic III. Can return 1 (for propset 1) or 2 (for propset 2), find your propcases here: http://chdk.wikia.com/wiki/PropertyCase This is one more command that belongs to the "universal category". get_zoom_steps Returns number of maximum zoom steps, irrespective of the processor. For example on the A620 it will return 8, on the S3IS it will return 128. This allows for universal zoom scripts. Example: for i=0 to get_zoom_steps ... do some things in this loop next i get_exp_count will return number of shots in a session (afaik). useful for counting when you are for example in burst mode and want to stop after 3 shots. On SD1000 returns file number of last exposure. get_drive_mode returns drivemode (as described on the property page) 0 = single 1 = continuous 2 = timer (on Digic II) 3 = timer (on Digic III) The timer issue will somewhat make it difficult to use it in universal scripts, however together with a get_propset this should be do-able. get_flash_mode returns flash mode (0,1,2 = flash auto, flash on, flash off) get_flash_ready returns if flash is ready and charged get_IS_mode returns IS mode 0,1,2,3 = continous, shoot only, panning, off get_orientation_sensor regardless of Digic II / Digic III it will return the orientation sensor's degrees. get_nd_present returns 0 if no ND Filter present, 1 if ND present and real diaphragm NOT present, 2 if both ND & diaphragm present. get_histo_range Usage: get_histo_range <'from'> <'to'> returns percent of values between and in histogram data. This command gets the information about the histogram of the last image that was shot (i.e. after you have focused, metered, shot etc.), it operates reading the RAW buffer (but doesn't require raw saving to be enabled: the shot histogram data is obtained from the RAW data before discarding or further processing it). It can be very useful to get informations about very dark scenes: the live preview screen is completely black, while the RAW buffer (with a long enough exposure) would contain an image. * It depends on the shot_histo_enable command: if shot_histo_enable is not set, get_histo_range returns a value of -1, so shot_histo_enable has to be enabled first. This is necessary because the shot-histogram is calculated just after each shot and takes some time. So, by default the shot-histogram is disabled and you need to enable it with shot_histo_enable. * Find more about this in this forum thread: Timelapse with variable shutter speed Sample: rem enable shooting histogram shot_histo_enable 1 rem shoot shoot sleep 100 rem let's read histogram of the shot that was just taken h=get_histo_range 100 800 rem h contains the percentage of pixels that are between 100/1024 and 800/1024 shot_histo_enable setting to 1 enables building of shot histogram data, 0 disables (see get_histo_range) autostarted returns 1 (true) if script was autostarted is_pressed "key" checks if a key is being pressed, when the command is called get_shooting returns propcase 205 (Digic II) / 206 (Digic III) get_ev returns propcase 25 (Digic II) / 107 (Digic III) set_ev sets propcases 25 & 26 (Digic II) / 107 & 207 (Digic III) get_autostart & set_autostart read and write conf.script_autostart (0=off, 1=on, 2=once) get_temperature "get_temperature 0", 0 returns optical, 1 returns CCD, and 2 returns battery temp :This can be used to both assign a variable or to print directly :Example: a = get_temperature 0 print "Optical Temp:",a print "Battery Temp:",get_temperature 2 select/case function :Here's a thread describing the use of the new uBASIC "select/case" feature: :http://chdk.setepontos.com/index.php/topic,1995.0.html Syntax: select EXPRESSION case EXPR. EXPR. | [,EXPR.2...]; STATEMENT . . STATEMENT end_select Sample: for x=1 to 7 select x case 1; gosub "new" case 7; goto "EXIT" case 2,4; print "x=2 | x=4" case 2 to 5; print "x=3 | x=5" case_else print"x=6" end_select next x :EXIT print "ready" end :new print "sub" for y=1 to 2 select y case 1; print "y=1" case_else print"y=2" end_select next y return changed RANDOM command :Now you can supply two values min & max :Example: "playsound random 3 6" will play the sounds 3,4,5,6 in random order (if repeated in a while loop) playsound :Plays any of the built-in sound events, when sounds are turned on. :Example: "playsound 0" ::Where 0 is the startup sound. There are sounds ranging from 0 to 7, 7 being a nasty long beep. The first few sounds can only be played if they are NOT muted by the camera, the other beeps will be played though (cam must be unmuted). This feature will lead to many more features, for example anti theft protection together with DataGhost's Disco lights. :0 = startup sound :1 = shutter sound :2 = button press sound :3 = selftimer :4 = short beep :5 = af (auto focus) confirmation :6 = error beep :7 = long beeeeeeeeeeeeeeeeeeep (nasty ! - can be stopped by half-pressing the shutter) *for Lua the command is play_sound *sound 7 plays even on a muted camera (tested on A610) print_screen This command now (since changeset #489) have a new behaviour: Log files are stored in the folder \CHDK\LOGS with a new naming scheme LOG_XXXX.TXT Usage: print_screen(123) (in Lua) or print_screen 123 (in uBasic) creates LOG_0123.TXT in the \LOGS folder *Logging is disabled by 'print_screen(false)' in Lua or 'print_screen 0' in uBasic *'print_screen(0)' in Lua or 'print_screen 10000' in uBasic writes to LOG_0000.TXT (as a side effect of the current implementation, also print_screen(-10000) disables writing) get_time (since changeset #497) Returns elements of the camera's current time. Usage: get_time 0''' (returns seconds), '''1 (minute), 2''' (hour), '''3 (day), 4''' (month), '''5 (year). Example: a=get_time 0 will set the value of "a" to the current number of seconds. (command also exists in Lua, though must be fed with CHARS, see Lua Reference) get_mode (since changeset #497) Returns whether record mode or playback mode is active, useful for closing scripts that are meant to be run in a certain mode (like the majority of all available scripts). Returns 0''' in record mode, '''1 in playback mode, 2''' when mode dial is set to videomode AND camera is in record mode (2 since changeset #499) *This command has advanced functionality in Lua, see Lua Reference: get_mode get_quality (since changeset #497) Returns the current capturing quality setting in Canon grades, regardless of the cameras OS *For possible values see PropertyCase (basically quality can be 0-2 where 2 being the worst) (not available in Lua yet) get_resolution (since changeset #497) Returns the current capturing resolution setting in Canon grades, regardless of the cameras OS *For possible values see PropertyCase (basically resolution can be 0-8, corresponding to the Canon settings) (not available in Lua yet) set_quality (since changeset #497) Set the quality (in Canon grades), usually 0-2, see PropertyCase for possible values (not available in Lua yet) set_resolution (since changeset #497) Set the resolution (in Canon grades), usually 0-8, see PropertyCase for possible values (not available in Lua yet) get_platform_id (since version 0.8.3 / changeset #593) Returns '''platformid (integer), with the help of this you can code platform depending scripts (in Lua we dont need this, as we can get strings there). ?'Platform-ID list ''(only available in uBasic; for Lua scripts use get_buildinfo instead) set_backlight() (since version 0.9.1 / changeset #679) With this function the LCD Backlight (or EVF backlight) can be turned on and off - useful for energy saving or camouflage missions. (Backlight is a bright light inside the LCD, making the image visible.) Usage: set_backlight(x) (Lua) or set_backlight x (uBASIC), where '''0 disables backlight and 1''' enables it. * Note: Camera enables backlight after each shot. To keep backlight mostly disabled, this command must be called after each shot. But do not call it immediately after a shot or its effect will be very shortlived. You must wait for LCD live view image exposure to settle before calling. The regular shoot state loop may help, possibly with some added delay. Test in varying lighting conditions to see if you got it right. * Note: If you accidentally exit a script while backlight is disabled, you can get it back by leaving ALT mode and shooting, or by powering off. The LCD can probably be somewhat read even when backlight is disabled if bright light is pointed at it from a suitable angle. Alternatively, you may turn on the LCD Backlight in a section labeled :restore (uBasic) or a function called restore (Lua), which will be executed when the script is interrupted. * An example script can be found here: http://chdk.setepontos.com/index.php/topic,2744.msg27703.html#msg27703 set_aflock (since version 0.9.2 / changeset #681) Lock/unlock autofocus Usage: Lua: set_aflock(x) uBASIC: set_aflock x where '''1 locks the autofocus and 0''' unlocks it. It is like MF, but better in several ways, such as a) it is available on ALL cameras; b) focus is locked even after camera returns from deep display sleep (via display key cycle or print button shortcut), which is very good for timelapse, with regard to power consumption, camera shake, an actual fixed focus throughout the whole timelapse and also less stress on the mechanical parts of the camera. Example: press "shoot_half" sleep 800 release "shoot_half" set_aflock 1 set_record(state) '''state 0 (or false) changes to play mode. 1 or true changes to record mode. NOTE 1: this only begins the mode change. Script should wait until get_mode() reflects the change before doing anything that requires the new mode. e.g. set_record(true) while not get_mode() do sleep(10) end NOTE 2: In ubasic, get_mode returns 0 for record, 1 for play and 2 for movie, so if you want save the state and restore it with set_record, you cannot just use the get_mode value directly. You can adjust the value from get_mode, or use get_capture_mode instead. (since version 0.9.8 / changeset #826) set_capture_mode_canon(value) value is a valid PROPCASE_SHOOTING_MODE value for the current camera. If the value is negative, it is assumed to be a sign-extended short representation of the propcase value and is converted accordingly. returns true (Lua only) if the camera is in rec mode. NOTE: On some cameras, a short sleep (10ms) may be required before the mode change is complete. (since version 0.9.9 / changeset #847) set_capture_mode(modenum) modenum is a valid mode in the CHDK modemap returns true (lua only) if modenum is a valid modemap value and the camera is in rec mode, otherwise false see also the Lua/Scripts:Standard/Lualib/Capmode standard module, which provides a more user friendly interface. NOTE: On some cameras, a short sleep (10ms) may be required before the mode change is complete. (since version 0.9.9 / changeset #847) WARNING This feature is still under development. The available mode numbers, their mapping to canon modes, and their symbolic names may change in the next several CHDK revisions. This notice will be removed when they are finalized. is_capture_mode_valid(modenum) modenum is a CHDK mode value returns is true if modenum is a a valid mode in the cameras modemap, otherwise false (since version 0.9.9 / changeset #847) get_capture_mode Ubasic only, not needed in lua, use get_mode instead. returns the current camera automatic image mode, or 0 if camera is in play mode or 0 if there is no modemap entry (since version 0.9.9 / changeset #847) set_console_layout New commands for the customizable script console, introduced with changeset ''#'899 ''but did not work prior to changeset #'''1314'' set_console_layout x1 y1 x2 y2 ' --> set console position and size, range is x=0 to 45 and y=0 to 14 set_console_autoredraw ''New commands for the customizable script console, introduced with changeset #899, updated in changeset #1394. '''set_console_autoredraw(-1|0|1')' --> enable or disable automatic rewrite/refresh of the console. Parameters: '-1' = log only (update to log file but not to display) ' 0' = disable ' 1' = enable (update to log file and display) console_redraw New commands for the customizable script console, introduced with changeset '#899''' '' console_redraw() --> manually refresh/rewrite the script console reboot(file) Reboot the the camera, optionally using a specified file. If file is not given, a normal the normal camera boot sequence is used, CHDK will be started or not based on the normal autostart rules. If the first two characters of the file extension are "FI" the file will be treated loaded as a firmware update file, encoded with the normal firmware update encoding. Currently, this only works on DryOS, vxworks cameras will just return without rebooting. In all other cases, the file must be an un-encoded binary, which is compiled to load at 0x1900. On success, reboot does not return. note reboot does not do a "nice" shutdown. * If the lens is extended, it will not be retracted before reboot. * Canon camera settings that are normally saved on shutddown are not saved. * If called from lua, the Lua script script will not be terminated normally, so any open files etc will not be closed. (since changeset #944 / FI* support in changeset 957) restore Both Lua and uBasic provide a mechanism for allowing the programmer to specify a piece of code to be executed immediately prior to the termination or interruption of a running program. This allows for such things as enabling the backlight if it was turned off or releasing overrides. To use this capability in uBasic, insert a label called :restore in your code (usually right before the end statement) and add the lines of code you would like executed as the program terminates or is interrupted. To use this capability in Lua, add a function called restore() to your Lua script with the commands you would like to see executed when Lua terminates or interrupts the currently running script. Note: This code will be called when the script is terminated by pressing the camera's shutter button. However, the code will wait for any sleep commands to finish before executing so it might be better to use short sleep commands inside a counting loop rather than one long sleep command (tested for uBASIC, assumed true for Lua). set_yield This command controls how CPU time is allocated to running scripts. You can adjust these values to allow complex scripts to run more quickly. This will cause less CPU to be available for other tasks, which may have side effects. Note that increasing these values will not make operations that happen outside of script code (e.g. shooting, motion detection) any faster. The script task yields (sleep for 10ms) when either condition is met. Values are treated as unsigned, so -1 can be used to effectively disable either condition. Commands that wait (such as sleep, wait_click, md_detect_motion etc) or send key presses yield immediately. ubasic usage: set_yield MAX_LINES is the maximum number of lines to execute in a single iteration of the script task. Default is 1, meaning that ubasic scripts take at least 10ms to execute each line. MAX_MS is the maximum number of milliseconds a single iteration of the script task is allowed to run for before yielding. Default is 10. The value has a resolution of 10ms, and is checked after each line. Since the resolution is 10ms, and the start of a script iteration does not necessarily begin in sync with the system clock, the actual time may be less than ten ms. Passing zero for either value resets it to default. lua usage: old_max_count,old_max_ms=set_yield(,) MAX_COUNT is the maximum number of lua VM instructions to execute in a single iteration of the script task, divided by 100. Default is 25, meaning a maximum of 2500 VM instructions will be executed. MAX_MS is as described for ubasic, except it is checked every 100 VM instructions rather than every line. The return values are the previous values of MAX_COUNT and MAX_MS. Libraries that need to adjust these values should save and restore the values. Passing nil for either value resets it to the default. In Lua, calls to lua code from within C (for example, using pcall()) cannot yield. However the VM calls and time will still be counted, so a yield will occur within 100 instructions after the C call if the limits are exceeded. (since changeset #1434, an incompatible variant for ubasic was introduced in changeset #1427) get_curve_file Lua commands to report on tone curve file currently loaded set_curve_file Lua commands to load a tone curve file get_curve_state Lua commands to determine if tone curve file is active or disabled. set_curve_state Lua commands to enable/disable tone curve file usage. ---- Category:Scripting Category:uBASIC Category:Lua